package com.src.computec.radar.services.utilities.zip;
/*
 * CreateZipFile.java
 *
 * Created on October 4, 2007, 10:55 AM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

	
	

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CreateZipFile {
  public static int BUFFER_SIZE = 10240;

  public void createZipArchive(File archiveFile, File[] tobeZippedFiles) {
    try {
      byte buffer[] = new byte[BUFFER_SIZE];
      // Open archive file
      FileOutputStream stream = new FileOutputStream(archiveFile);
      ZipOutputStream out = new ZipOutputStream(stream);

      for (int i = 0; i < tobeZippedFiles.length; i++) {
        if (tobeZippedFiles[i] == null || !tobeZippedFiles[i].exists()
            || tobeZippedFiles[i].isDirectory())
          continue;
        System.out.println("Adding " + tobeZippedFiles[i].getName());

        // Add archive entry
        ZipEntry zipAdd = new ZipEntry(tobeZippedFiles[i].getName());
        zipAdd.setTime(tobeZippedFiles[i].lastModified());
        out.putNextEntry(zipAdd);

        // Read input & write to output
        FileInputStream in = new FileInputStream(tobeZippedFiles[i]);
        while (true) {
          int nRead = in.read(buffer, 0, buffer.length);
          if (nRead <= 0)
            break;
          out.write(buffer, 0, nRead);
        }
        in.close();
      }

      out.close();
      stream.close();
      System.out.println("Adding completed OK");
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println("Error: " + e.getMessage());
      return;
    }
  }
  
   public void createZipArchive(File archiveFile, File tobeZippedFiles) {
    try {
      byte buffer[] = new byte[BUFFER_SIZE];
      // Open archive file
      FileOutputStream stream = new FileOutputStream(archiveFile);
      ZipOutputStream out = new ZipOutputStream(stream);

     
        if (tobeZippedFiles!= null && tobeZippedFiles.exists()
            && !tobeZippedFiles.isDirectory()){
          
      //  System.out.println("Adding " + tobeZippedFiles.getName());

        // Add archive entry
        ZipEntry zipAdd = new ZipEntry(tobeZippedFiles.getName());
        zipAdd.setTime(tobeZippedFiles.lastModified());
        out.putNextEntry(zipAdd);

        // Read input & write to output
        FileInputStream in = new FileInputStream(tobeZippedFiles);
        while (true) {
          int nRead = in.read(buffer, 0, buffer.length);
          if (nRead <= 0)
            break;
          out.write(buffer, 0, nRead);
        }
        in.close();
      
	}
      out.close();
      stream.close();
     // System.out.println("Adding completed OK");
    } catch (Exception e) {
      e.printStackTrace();
      //System.out.println("Error: " + e.getMessage());
      return;
    }
  }
   
}
